package com.extremelylargepackage;
import com.extremelylargepackage.editor.LevelEditorActivity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;


public class SchmupView extends SurfaceView implements SurfaceHolder.Callback
{
	public enum EngineType{ menu, game, pause, levelSelect, garage }
	
	class SchmupThread extends Thread implements ThreadCallBack
	{
		private Context _context;
		private Engine _engine;
		private SurfaceHolder _surfaceHolder;
		private Handler _handler;
		private boolean _isRunning;
		private boolean _surfaceAlive;
		private boolean _visible;
		
		public SchmupThread(SurfaceHolder holder, Context context, Handler handler)
		{
			_surfaceHolder = holder;
			_context = context;
			_handler = handler;
			_visible = true;
		}
		
		public void loadResources()
		{
			GameResources.setResources(_context.getResources());
			GameResources.loadResources();
			GameResources.loadProjectiles();
			GameResources.loadWeapons();
			GameResources.loadEnemies();
		}
		
		public void startGameEngine(String levelName, boolean local)
		{
			_engine = new GameEngine(this, levelName, local);
			_engine.initialize();
		}
		
		public void startEngine(EngineType type, boolean custom)
		{
			switch(type)
			{
				case menu:
					_engine = new MenuEngine(this);
					break;
				case pause:
					break;
				case levelSelect:
					_engine = new LevelSelectEngine(this, custom);
					break;
				case garage:
					_engine = new GarageEngine(this);
				default:
					break;
			}
			_engine.initialize();
		}
		
		public void pauseGame()
		{
			if(_engine != null)
				_engine.setPaused(true);
		}
		
		public void resumeGame()
		{
			if(_engine != null)
				_engine.setPaused(false);
		}
		
		@Override
		public void run()
		{
			Canvas c = null;
			while(_isRunning)
			{
				try
				{
					if(_surfaceAlive)
						c = _surfaceHolder.lockCanvas();
					else
						c = null;
					synchronized(_surfaceHolder)
					{
						if(_engine != null)
						{
							_engine.gameLogic();
							if(c != null)
								_engine.draw(c);
						}
					}
				}
				finally
				{
					if(c != null)
						_surfaceHolder.unlockCanvasAndPost(c);
				}
				//Don't hog the CPU when in the background.
				while(!_visible)
				{
					try {
						Thread.sleep(100);
					} catch (InterruptedException e) {
						//Do nothing
					}
				}
			}
		}
		
		public void setSurfaceAlive(boolean alive)
		{
			_surfaceAlive = alive;
		}
		
		public boolean onTouch(MotionEvent event)
		{
			if(_engine != null)
				_engine.onTouch(event);
			return true;
		}

		@Override
		public void startGame(String levelName, boolean local) 
		{
			Runtime.getRuntime().gc();
			Intent intent = new Intent(_context, GameActivity.class);
			Bundle bundle = new Bundle();
			bundle.putString("level", levelName);
			bundle.putBoolean("local", local);
			intent.putExtras(bundle);
			_context.startActivity(intent);
		}

		public void saveGameState() {
			((GameEngine)_engine).saveState(_context);
		}
		
		@Override
		public void startLevelSelect(boolean custom)
		{
			Intent intent = new Intent(_context, MultiActivity.class);
			Bundle bundle = new Bundle();
			bundle.putString("engine", "levelSelect");
			bundle.putBoolean("custom", custom);
			intent.putExtras(bundle);
			_context.startActivity(intent);
		}
		
		@Override
		public void startGarage() 
		{
			Intent intent = new Intent(_context, LevelEditorActivity.class);
			Bundle bundle = new Bundle();
			//bundle.putString("engine", "garage");
			intent.putExtras(bundle);
			_context.startActivity(intent);
		}
		
		@Override
		public void levelFinished(boolean victory)
		{
			((Activity)_context).finish();
		}
		
		public void setRunning(boolean run)
		{
			_isRunning = run;
		}
		
		public void setVisible(boolean visible)
		{
			_visible = visible;
		}
	}
	
	private SchmupThread _thread;
	private Context _context;

	public SchmupView(Context context, AttributeSet attrs)
	{
		super(context, attrs);
		SurfaceHolder holder = this.getHolder();
		holder.addCallback(this);
		
		_thread = new SchmupThread(holder, context, new Handler(){
			public void handleMessages(Message m)
			{  
				
			}
		});
		setFocusable(true);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event)
	{
		return _thread.onTouch(event);
	}
	
	public SchmupThread getThread()
	{
		return _thread;
	}
	
	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) 
	{
		// TODO Auto-generated method stub
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) 
	{
		_thread.setRunning(true);
		if(!_thread.isAlive())
			_thread.start();
		_thread.setSurfaceAlive(true);
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) 
	{
		_thread.setSurfaceAlive(false);
	}
}
